﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Littlebeer.Common.FieldMapping
{
    public class MappingManager
    {
        List<FieldPairModel> _maps = new List<FieldPairModel>();

        public List<FieldPairModel> Maps
        {
            get { return _maps; }
            set { _maps = value; }
        }
        public MappingManager()
        {
            _maps = new List<FieldPairModel>();
        }
        public void Add(FieldPairModel pair)
        {
            if (Exist(pair))
            {
                throw (new Exception("映射关系已经存在！"));
            }
            else
            {
                Maps.Add(pair);
            }
        }
        public Boolean Exist(FieldPairModel pair)
        {
            Boolean result = false;
            result = Maps.Any(d => d.Left == pair.Left || d.Right ==pair.Right);
            return result;
        }
        public string GetSourceFieldName(string targetFieldName,List<FieldPairModel> Maps)
        {
            string result = "";
            foreach (FieldPairModel item in Maps)
            {
                if (item.Right.FieldName == targetFieldName)
                {
                    return  item.Left.FieldName;
                }
            }
            if (result == "")
            {
                throw (new Exception("找不到对应的字段名称。"));
            }
            return result;
        }
        public string GetTargetFieldName(string sourceFieldName, List<FieldPairModel> Maps)
        {
            string result = "";
            foreach (FieldPairModel item in Maps)
            {
                if (item.Left.FieldName == sourceFieldName)
                {
                    return item.Right.FieldName;
                }
            }
            if (result == "")
            {
                throw (new Exception("找不到对应的字段名称。"));
            }
            return result;
        }
    }
}
